﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2012r
/// 파  일  명 : Mny2012r.aspx.cs
/// 설      명 : 전체출납챠트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-08-07 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Mny.Exe;


namespace Updms.Mny
{
	public partial class Mny2012r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2012r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);

		//검색
		protected string kw1 = "";	//base_from_dt
		protected string kw2 = "";	//base_to_dt
		protected string kw3 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetYearMonth();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";
			
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();

			gs_page_mode = (Request.QueryString["page_mode"] == null || Request.QueryString["page_mode"].Trim().Length == 0)
					? "" : Request.QueryString["page_mode"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_from_year.SelectedValue = kw1.Substring(0, 4).ToString();
						ddl_search_from_month.SelectedValue = kw1.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_to_year.SelectedValue = kw2.Substring(0, 4).ToString();
						ddl_search_to_month.SelectedValue = kw2.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_chart_width.SelectedValue = kw3;
					}

					break;
				case "SEARCH":
					kw1 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					kw2 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					kw3 = ddl_search_chart_width.SelectedValue;
					break;
			}
		}
		
		#endregion

		#region 마스터 Method (SetYearMonth, SetChartWidth)

		// 검색조건의 년월설정
		private void SetYearMonth()
		{
			ddl_search_from_year.Items.Clear();
			ddl_search_to_year.Items.Clear();

			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_from_year.Items.Add(new ListItem(ls_year, ls_year));
				ddl_search_to_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			SetYearMonth(-12);
		}

		// 검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			ddl_search_from_year.SelectedValue = DateUtil.GetMonthEx(li_term, "yyyy"); //x개월전년도
			ddl_search_from_month.SelectedValue = DateUtil.GetMonthEx(li_term, "MM"); //x개월전월
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}
		
		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);

			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion

		#region 챠트 (DrawTotalInOutList, DrawMonthlyCarryoverDiffAmt, DrawYearlyOutAmountListByBudget, SearchProcess)

		//수입지출
		private void DrawTotalInOutList()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//초기화
			chart_line_total.Data.Clear();

			//시리즈
			chart_line_total.Data.Series = 3;
			
			//넓이
			chart_line_total.Width = Convert.ToInt32(kw3);

			//검색
			lo_dp.AddProperty("ls_base_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_dt_to", typeof(string), kw2);

			using (Mny2012r_NT _Mny2012r_NT = new Mny2012r_NT())
			{
				lo_ds.Merge(_Mny2012r_NT.GetTotalInOutList(lo_dp));
			}

			int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_line_total.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "InAmount");
				chart_line_total.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "OutAmount");
				chart_line_total.Data[2, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "SaveAmount");

				//X축 라벨링
				string ls_base_dt = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Base_Dt").Substring(2, 5).Replace("-", ".");
				chart_line_total.AxisX.Labels[i] = ls_base_dt;
			}
		}

		//이월차액
		private void DrawMonthlyCarryoverDiffAmt()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//초기화
			chart_margin.Data.Clear();

			//시리즈
			chart_margin.Data.Series = 1;

			//넓이
			chart_margin.Width = Convert.ToInt32(kw3);

			//CustomGridLine
			ChartFX.WebForms.CustomGridLine cgl1 = new ChartFX.WebForms.CustomGridLine();
			cgl1.Value = 0;
			cgl1.Color = System.Drawing.Color.Red;
			cgl1.Width = 1;
			chart_margin.AxisY.CustomGridLines.Add(cgl1);

			//검색
			lo_dp.AddProperty("ls_base_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_dt_to", typeof(string), kw2);

			using (Mny2012r_NT _Mny2012r_NT = new Mny2012r_NT())
			{
				lo_ds.Merge(_Mny2012r_NT.GetMonthlyCarryoverDiffAmt(lo_dp));
			}

			int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_margin.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Amount");

				//X축 라벨링
				string ls_base_dt = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Base_Dt").Substring(2, 5).Replace("-", ".");
				chart_line_total.AxisX.Labels[i] = ls_base_dt;
				chart_margin.AxisX.Labels[i] = ls_base_dt;

				// 마이너스(-) 금액은 구분되도록
				chart_margin.Points[0, i].Color = (chart_margin.Data[0, i] < 0) ? System.Drawing.Color.Plum : System.Drawing.Color.FromArgb(81, 121, 214);
			}
		}

		//예산대비지출
		private void DrawYearlyOutAmountListByBudget()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//초기화
			chart_out_by_budget.Data.Clear();

			//시리즈 갯수
			chart_out_by_budget.Data.Series = 3; //예산, 지출+저축, 예산-(지출+저축)

			//넓이
			chart_out_by_budget.Width = Convert.ToInt32(kw3);

			//아래 Pane의 커스텀라벨
			ChartFX.WebForms.CustomGridLine cgl1 = new ChartFX.WebForms.CustomGridLine(0, "");
			cgl1.Color = System.Drawing.Color.Gray;
			cgl1.Width = 1;
			chart_out_by_budget.Panes[1].AxisY.CustomGridLines.Add(cgl1);
			chart_out_by_budget.Panes[1].AxisY.LabelsFormat.CustomFormat = "#,###,##0";

			//검색
			lo_dp.AddProperty("ls_base_dt_from", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_dt_to", typeof(string), kw2);

			using (Mny2012r_NT _Mny2012r_NT = new Mny2012r_NT())
			{
				lo_ds.Merge(_Mny2012r_NT.GetTotalOutAmountListByBudget(lo_dp));
			}

			int li_cnt = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

			for (int i = 0; i < li_cnt; i++)
			{
				//수동으로 데이터 바인딩
				chart_out_by_budget.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Budget");
				chart_out_by_budget.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Out_amt");
				chart_out_by_budget.Data[2, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance");

				//X축 라벨링
				string ls_base_dt = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Base_Dt").Substring(2, 5).Replace("-", ".");
				chart_out_by_budget.AxisX.Labels[i] = ls_base_dt;

				// 마이너스(-) 금액은 구분되도록
				chart_out_by_budget.Points[2, i].Color = (chart_out_by_budget.Data[2, i] < 0) ? System.Drawing.Color.Red : System.Drawing.Color.FromArgb(81, 121, 214);
			}
		}

		//검색
		private void SearchProcess()
		{
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			
			DrawTotalInOutList();
			DrawMonthlyCarryoverDiffAmt();
			DrawYearlyOutAmountListByBudget();
		}

		#endregion	

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}
		
		//ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		//처음 페이지로드시
					Top.Visible = true;
					menu_area.Attributes.Add("class", "");
					ibtn_close.Visible = false;
					break;
				case "P":		//팝업으로 열때
					Top.Visible = false;
					menu_area.Attributes.Add("class", "nodisplay");
					ibtn_close.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-5);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			ddl_search_from_year.SelectedIndex = 0;
			ddl_search_from_month.SelectedIndex = 0;
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}

		//검색
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}
			
		#endregion

	}
}
